﻿using LogLib;
using MetroFramework.Forms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NewCapturerDemo
{
    public partial class FormCameraSetting : MetroForm
    {
        private Camera _camera;

        private CamProperties properties;

        public event EventHandler<CamPropValUpdatedEventArgs> CamPropertyUpdated = null;

        private void OnPropertyUpdated(CamProc type, double value)
        { 
            if(CamPropertyUpdated != null)
            {
                CamPropertyUpdated(this, new CamPropValUpdatedEventArgs(type, value));
            }
        }

        public FormCameraSetting(Camera capture)
        {
            _camera = capture;
            properties = capture.Properties;
            InitializeComponent();
        }

        private void LoadSetting()
        {
            properties.GetDefaultCamProps();
            properties.GetCurrCamPropValue();
        }

        private void UpdateGUI()
        {
            rangeTrackBarBrightness.MinValue = properties[CamProc.Brightness].Min;
            rangeTrackBarBrightness.MaxValue = properties[CamProc.Brightness].Max;
            rangeTrackBarBrightness.Value = properties[CamProc.Brightness].Value;

            rangeTrackBarContrast.MinValue = properties[CamProc.Contrast].Min;
            rangeTrackBarContrast.MaxValue = properties[CamProc.Contrast].Max;
            rangeTrackBarContrast.Value = properties[CamProc.Contrast].Value;

            rangeTrackBarHue.MinValue = properties[CamProc.Hue].Min;
            rangeTrackBarHue.MaxValue = properties[CamProc.Hue].Max;
            rangeTrackBarHue.Value = properties[CamProc.Hue].Value;

            rangeTrackBarStaturation.MinValue = properties[CamProc.Saturation].Min;
            rangeTrackBarStaturation.MaxValue = properties[CamProc.Saturation].Max;
            rangeTrackBarStaturation.Value = properties[CamProc.Saturation].Value;

            rangeTrackBarSharpness.MinValue = properties[CamProc.Sharpness].Min;
            rangeTrackBarSharpness.MaxValue = properties[CamProc.Sharpness].Max;
            rangeTrackBarSharpness.Value = properties[CamProc.Sharpness].Value;

            rangeTrackBarGamma.MinValue = properties[CamProc.Gamma].Min;
            rangeTrackBarGamma.MaxValue = properties[CamProc.Gamma].Max;
            rangeTrackBarGamma.Value = properties[CamProc.Gamma].Value;

            rangeTrackBarFocus.MinValue = properties[CamProc.Focus].Min;
            rangeTrackBarFocus.MaxValue = properties[CamProc.Focus].Max;
            rangeTrackBarFocus.Value = properties[CamProc.Focus].Value;
            metroCheckBoxFocus.Checked = properties[CamProc.Focus].Auto;

            rangeTrackBarExposure.MinValue = properties[CamProc.Exposure].Min;
            rangeTrackBarExposure.MaxValue = properties[CamProc.Exposure].Max;
            rangeTrackBarExposure.Value = properties[CamProc.Exposure].Value;
            metroCheckBoxExp.Checked = properties[CamProc.Exposure].Auto;
            rangeTrackBarExposure.Enabled = !properties[CamProc.Exposure].Auto;

            rangeTrackBarWhiteBalance.MinValue = properties[CamProc.WhiteBalance].Min;
            rangeTrackBarWhiteBalance.MaxValue = properties[CamProc.WhiteBalance].Max;
            rangeTrackBarWhiteBalance.Value = properties[CamProc.WhiteBalance].Value;
            metroCheckBoxWB.Checked = properties[CamProc.WhiteBalance].Auto;
            rangeTrackBarWhiteBalance.Enabled = !properties[CamProc.WhiteBalance].Auto;
        }

        private void GetSettings()
        {
            properties[CamProc.Brightness].Value = (int)rangeTrackBarBrightness.Value;
            properties[CamProc.Contrast].Value = (int)rangeTrackBarContrast.Value;
            properties[CamProc.Hue].Value = (int)rangeTrackBarHue.Value;
            properties[CamProc.Saturation].Value = (int)rangeTrackBarStaturation.Value;
            properties[CamProc.Sharpness].Value = (int)rangeTrackBarSharpness.Value;
            properties[CamProc.Gamma].Value = (int)rangeTrackBarGamma.Value;
            properties[CamProc.Focus].Value = (int)rangeTrackBarFocus.Value;
            properties[CamProc.Exposure].Value = (int)rangeTrackBarExposure.Value;
            properties[CamProc.Exposure].Auto = metroCheckBoxExp.Checked;
            properties[CamProc.WhiteBalance].Value = (int)rangeTrackBarWhiteBalance.Value;
            properties[CamProc.WhiteBalance].Auto = metroCheckBoxWB.Checked;
        }

        private void SetSettings()
        {
            if(_camera == null)
            {
                return;
            }   
        }

        /// <summary>
        /// 保存相机参数到文件
        /// </summary>
        private void SaveSettings()
        {
            properties.SaveCurrCamPropValue();
        }

        private void FormCameraSetting_Load(object sender, EventArgs e)
        {
            Log.AddBeginningBoundary();
            Log.WriteAsync("Enter 相机参数设置");
            LoadSetting();
            Log.WriteAsync("设置前的相机参数为:");
            string[] strArray = properties.StringValues();
            foreach (string item in strArray)
            {
                Log.WriteAsync(item);
            }
            UpdateGUI();
        }

        private void rangeTrackBarbacklight_ValueChanged(object sender, EventArgs e)
        {
            _camera.SetBacklightCompensation(rangeTrackBarbacklight.Value);
            OnPropertyUpdated(CamProc.Backlight, rangeTrackBarbacklight.Value);
        }

        private void rangeTrackBarBrightness_ValueChanged(object sender, EventArgs e)
        {
            _camera.SetBrightness(rangeTrackBarBrightness.Value);
            OnPropertyUpdated(CamProc.Brightness, rangeTrackBarBrightness.Value);
        }

        private void rangeTrackBarContrast_ValueChanged(object sender, EventArgs e)
        {
            _camera.SetContrast(rangeTrackBarContrast.Value);
            OnPropertyUpdated(CamProc.Contrast, rangeTrackBarContrast.Value);
        }

        private void rangeTrackBarStaturation_ValueChanged(object sender, EventArgs e)
        {
            _camera.SetSaturation(rangeTrackBarStaturation.Value);
            OnPropertyUpdated(CamProc.Saturation, rangeTrackBarStaturation.Value);
        }

        private void rangeTrackBarSharpness_ValueChanged(object sender, EventArgs e)
        {
            _camera.SetSharpness(rangeTrackBarSharpness.Value);
            OnPropertyUpdated(CamProc.Sharpness, rangeTrackBarSharpness.Value);
        }

        private void rangeTrackBarHue_ValueChanged(object sender, EventArgs e)
        {
            _camera.SetHue(rangeTrackBarHue.Value);
            OnPropertyUpdated(CamProc.Hue, rangeTrackBarHue.Value);
        }

        private void rangeTrackBarGamma_ValueChanged(object sender, EventArgs e)
        {
            _camera.SetGamma(rangeTrackBarGamma.Value);
            OnPropertyUpdated(CamProc.Gamma, rangeTrackBarGamma.Value);
        }

        private void rangeTrackBarWhiteBalance_ValueChanged(object sender, EventArgs e)
        {
            if (!metroCheckBoxWB.Checked)
            {
                _camera.SetWhiteBalance(rangeTrackBarWhiteBalance.Value);
                OnPropertyUpdated(CamProc.WhiteBalance, rangeTrackBarWhiteBalance.Value);
            }
        }

        private void rangeTrackBarGain_ValueChanged(object sender, EventArgs e)
        {
            _camera.SetGain(rangeTrackBarGain.Value);
            OnPropertyUpdated(CamProc.Gain, rangeTrackBarGain.Value);
        }

        private void rangeTrackBarFocus_ValueChanged(object sender, EventArgs e)
        {
            if (!metroCheckBoxFocus.Checked)
            {
                _camera.SetFocus(rangeTrackBarFocus.Value);
                OnPropertyUpdated(CamProc.Focus, rangeTrackBarFocus.Value);
            }
        }

        private void rangeTrackBarExposure_ValueChanged(object sender, EventArgs e)
        {
            if (!metroCheckBoxExp.Checked)
            {
                _camera.SetExposure(rangeTrackBarExposure.Value);
                OnPropertyUpdated(CamProc.Exposure, rangeTrackBarExposure.Value);
            }            
        }

        private void buttonCancell_Click(object sender, EventArgs e)
        {
            this.Close();
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            GetSettings();
            SaveSettings();         
            Log.WriteAsync("设置后的相机参数为:");
            string[] strArray = properties.StringValues();
            foreach (string item in strArray)
            {
                Log.WriteAsync(item);
            }
            Log.WriteAsync("End 相机参数设置");
            Log.AddEndingBoundary();    
            this.Close();
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void metroCheckBoxExp_CheckedChanged(object sender, EventArgs e)
        {
            rangeTrackBarExposure.Enabled = !metroCheckBoxExp.Checked;
            _camera.SetExposure(rangeTrackBarExposure.Value, metroCheckBoxExp.Checked);
            double val = _camera.GetExposure();
            rangeTrackBarExposure.Value = (int)val;
        }

        private void metroCheckBoxWB_CheckedChanged(object sender, EventArgs e)
        {
            rangeTrackBarWhiteBalance.Enabled = !metroCheckBoxWB.Checked;
            _camera.SetWhiteBalance(rangeTrackBarWhiteBalance.Value, metroCheckBoxWB.Checked);
            double val = _camera.GetWhiteBalance();
            rangeTrackBarWhiteBalance.Value = (int)val;
        }

        private void metroCheckBoxFocus_CheckedChanged(object sender, EventArgs e)
        {
            rangeTrackBarFocus.Enabled = !metroCheckBoxFocus.Checked;
            _camera.SetFocus(rangeTrackBarFocus.Value, metroCheckBoxFocus.Checked);
            double val = _camera.GetFocus();
            rangeTrackBarFocus.Value = (int)val;
        }
    }
}
